package com.mysoft.gfyx.workbench.service.impl;

import cn.hutool.core.lang.UUID;
import com.mysoft.framework.cache.api.CacheProvider;
import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.gfyx.common.constants.AuthConstant;
import com.mysoft.gfyx.workbench.controller.dto.GetUserTokenResponseDto;
import com.mysoft.gfyx.workbench.mip.YcgApiMipService;
import com.mysoft.gfyx.workbench.mip.dto.*;
import com.mysoft.gfyx.workbench.service.YcgService;
import com.mysoft.gfyx.workbench.service.dto.UserBusinessOpportunityRequestDto;
import com.mysoft.gfyx.workbench.service.dto.UserCodeTokenDto;
import com.mysoft.gfyx.workbench.service.dto.UserGetTokenRequestDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class YcgServiceImpl implements YcgService {

    @Autowired
    YcgApiMipService ycgApiMipService;

    @Autowired
    private CacheProvider cacheProvider;

    @Override
    public Object getUserBusinessOpportunity(UserBusinessOpportunityRequestDto requestDto) {
        BasicYcgListResponseDto Response = ycgApiMipService.GetUserBusinessOpportunity(requestDto.getAuthToken(), requestDto.getPageSize(), requestDto.getPage());
        return Response.getData();
    }

    @Override
    public Object getSupplierUserAuthToken(UserGetTokenRequestDto requestDto) {
        GetUserAuthTokenRequestDto mipRequestDto = new GetUserAuthTokenRequestDto();
        mipRequestDto.setMobile(requestDto.getMobile());
        mipRequestDto.setSaas_code(requestDto.getSaasCode());
        mipRequestDto.setTenant_code(requestDto.getTenantCode());
        GetUserAuthTokenResponseDto responseDto = ycgApiMipService.getSupplierTokenBySaas(mipRequestDto);

        if (!responseDto.getSuccess()) {
            // 即使报错了，也不能抛出异常
             GetUserAuthTokenDto authToken = new GetUserAuthTokenDto();
             authToken.setToken("");
             responseDto.setData(authToken);
            // throw new BusinessLogicException(responseDto.getMessage());
        }

        return responseDto.getData();
    }

    @Override
    public Object getSupplierUserSsoUrl(UserGetTokenRequestDto requestDto) {
        GetUserAuthTokenRequestDto mipRequestDto = new GetUserAuthTokenRequestDto();
        mipRequestDto.setMobile(requestDto.getMobile());
        mipRequestDto.setSaas_code(requestDto.getSaasCode());
        mipRequestDto.setTenant_code(requestDto.getTenantCode());
        mipRequestDto.setTarget_url(requestDto.getTargetUrl());
        GetUserSsoUrlResponseDto responseDto = ycgApiMipService.getSupplierUserSsoUrl(mipRequestDto);

        if (!responseDto.getSuccess()) {
            // 处理错误信息
            GetUserSsoUrlDto dto = new GetUserSsoUrlDto();
            dto.setUrl(requestDto.getTargetUrl());
            responseDto.setData(dto);
        }

        return responseDto.getData();
    }

    @Override
    public GetUserTokenResponseDto getUserToken(String userCode) {
        GetUserTokenResponseDto getUserTokenResponseDto = new GetUserTokenResponseDto();

        String token = UUID.fastUUID().toString();
        String cacheKey = AuthConstant.TOKEN_PREFIX + token;

        UserCodeTokenDto userCodeTokenDto = new UserCodeTokenDto();
        userCodeTokenDto.setUserCode(userCode);

        cacheProvider.set(cacheKey, userCodeTokenDto, 57600);

        getUserTokenResponseDto.setToken(token);

        return getUserTokenResponseDto;
    }
}
